module Holo
  class Command
    class IPC < Command
      class Edit < IPC
        class Customization < Edit
          self.summary = 'edit App/customization.yml'

          self.description = <<-DESC
          ……
          DESC

          def self.options
            [
              ['--pod_name', 'pod name'],
              ['--pod_path', 'pod path'],
              ['--customization', '.custonmization.yml path']
            ].concat(super)
          end

          def initialize(argv)
            @pod_name = argv.option('pod_name')
            @pod_path = argv.option('pod_path')
            @customization = argv.option('customization')
            super
          end

          # bundle exec holo ipc edit custonmization \
          # --pod_name=XZHNetwork \
          # --pod_path=/Users/xiongzenghui/Desktop/xzhnetwork  \
          # --customization=/Users/xiongzenghui/Desktop/app_zh/.customization.yml
          def run
            require 'yaml'

            map = if File.exists?(@customization)
                    YAML.safe_load(File.read(@customization))
                  else
                    {}
                  end
            map ||= {}

            map[@pod_name] = {
              'path' => @pod_path
            }

            File.open(@customization, 'w+') do |file| file.write(map.to_yaml) end
          end
        end
      end
    end
  end
end